﻿using Data;
using LoginUI.Login;
using LoginUI.UI;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Login.Server
{
    public class ServerPanel : BasePanel
    {
        public Button btnStart;
        public Button btnChange;
        public Button btnBack;

        public Text txtName;
        private ServerInfo _serverInfo;

        public override void Init()
        {
            var id = LoginMgr.Instance.LoginData.lastServerId;
            var serverInfo = LoginMgr.Instance.FindServerInfo(id);
            ChangeInfo(serverInfo);
            btnStart.onClick.AddListener(StartHandle);
            btnChange.onClick.AddListener(ChangeHandle);
            btnBack.onClick.AddListener(BackHandle);
        }

        private void StartHandle()
        {
            UIManager.Instance.HidePanel<ServerPanel>();
            UIManager.Instance.HidePanel<LoginBKPanel>();
            LoginMgr.Instance.LoginData.lastServerId = _serverInfo.id;
            LoginMgr.Instance.SaveLoginData();
            SceneManager.LoadScene("GameScene");
        }

        private void ChangeHandle()
        {
            UIManager.Instance.HidePanel<ServerPanel>();
            UIManager.Instance.ShowPanel<ChooseServerPanel>();
        }

        private void BackHandle()
        {
            if (LoginMgr.Instance.LoginData.isAuto) LoginMgr.Instance.LoginData.isAuto = false;
            UIManager.Instance.HidePanel<ServerPanel>();
            UIManager.Instance.ShowPanel<LoginPanel>();
        }

        public void ChangeInfo(ServerInfo serverInfo)
        {
            _serverInfo = serverInfo;
            txtName.text = serverInfo.id + "区 - " + serverInfo.name;
        }
    }
}